/*
 * BRANCH.c
 *
 *  Created on: Jan 19, 2014
 *      Author: adijoshi
 */
//INCLUDES
#include "includes.h"
/*
 * Function: func_BRANCH
 *
 * Inputs:	Mnemonic
 * Outputs:  	1 - take branch
 * 				0 - do not take branch
 *
 * 		aLL BRANCH instructions
 *
 */

int func_BRANCH(int mnemonic)
{
	int z_flag, n_flag, v_flag, c_flag;
	int return_value;

	n_flag = get_N_flag();
	z_flag = get_Z_flag();
	v_flag = get_V_flag();
	c_flag = get_C_flag();


	switch(mnemonic)
	{
		case JMP:
		case BR:
			return_value =  0;
			break;

		case BNE:
			if(z_flag == 0)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BEQ:
			if(z_flag == 1)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BGE:
			if((n_flag ^ v_flag) == 0)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BLT:
			if((n_flag ^ v_flag) == 1)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BGT:
			if((z_flag | (n_flag ^ v_flag)) == 0)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BLE:
			if((z_flag | (n_flag ^ v_flag)) == 1)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BPL:
			if(n_flag == 0)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BMI:
			if(n_flag == 1)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BHI:
			if((c_flag | z_flag) == 0)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BLOS:
			if((c_flag | z_flag) == 1)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BVC:
			if(v_flag == 0)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BVS:
			if(v_flag == 1)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BCC:
			if(c_flag == 0)
				return_value =  1;
			else
				return_value =  0;
			break;

		case BCS:
			if(c_flag == 1)
				return_value =  1;
			else
				return_value =  0;
			break;
	}
	return return_value;
}
